import { Body, Controller, Post } from "@nestjs/common";
import { getHistoryDto, sendMessageDto } from "src/dto/message.dto";
import { success, getTimeStamp } from "src/utils/common";
import { MessageService } from "./message.service";

@Controller("message")
export class MessageController {
  constructor(private messageService: MessageService) { }

  @Post("getHistory")
  async getHistory(@Body() historyInfo: getHistoryDto) {
    const data = await this.messageService.getHistory(historyInfo.room_id);
    return success("请求成功", data);
  }
  @Post("sendText")
  sendText(@Body() message: sendMessageDto) {
    //避免暴露敏感信息
    const user = message.user;
    user.user_password = undefined;
    user.user_status = undefined;
    user.user_name = undefined;
    user.user_remark = undefined;
    const messageData = {
      message_content: JSON.stringify({
        form: message.user,
        content: message.message,
      }),
      message_room: message.room_id,
      message_time: getTimeStamp(),
      message_type: 0,
      message_user: user.user_id,
    };

    this.messageService.sendMessage(message.room_id, messageData);
    return "";
  }
}
